//
//  CircleControlView.m
//  HelloJoy
//
//  Created by Leone Keijzer on 07/09/2011.
//  Copyright (c) 2011 Cameraland. All rights reserved.
//

#import "CircleControlView.h"

@implementation CircleControlView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        initializedCenter = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    if(!initializedCenter){
        centerPoint.x = ( self.bounds.size.width / 2 );
        centerPoint.y = ( self.bounds.size.height / 2);
        initializedCenter = YES;
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetShouldAntialias(context, true);
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextSetAlpha(context, 0.2);
    //Set ellipse
    CGContextFillEllipseInRect(context, CGRectInset(self.bounds, 5, 5));
    //Set alpha of ellipse
    CGContextSetAlpha(context, 0.7);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(context, 2.0);
    //Set ellipse for button
    CGContextStrokeEllipseInRect(context, CGRectInset(self.bounds, 5, 5));
    CGContextSetAlpha(context, 0.5);
    
    CGRect ballRect;
    ballRect.origin.x = centerPoint.x - 20;
    ballRect.origin.y = centerPoint.y - 20;
    ballRect.size.width = 40;
    ballRect.size.height = 40;
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(context, 1.0);
    CGContextFillEllipseInRect(context, ballRect);
}

#pragma mark - Interaction control

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    centerPoint = [[touches anyObject] locationInView:self];
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    centerPoint.x = (self.bounds.size.width / 2);
    centerPoint.y = (self.bounds.size.height / 2);
    [self setNeedsDisplay];
}

#pragma mark - Accessors

- (double)getDistance {
    double d = 0.0;
    CGPoint circCenter;
    circCenter.x = (self.bounds.size.width / 2);
    circCenter.y = (self.bounds.size.height / 2);
    double d1 = (circCenter.x - centerPoint.x) * (circCenter.x - centerPoint.x);
    double d2 = (circCenter.y - centerPoint.y) * (circCenter.y - centerPoint.y);
    d = sqrt(d1 + d2);
    d = d / (double)(self.bounds.size.width / 2);
    
    return d;
}

- (double)getAngle {
    double angle = 0.0;
    CGPoint circCenter;
    circCenter.x = (self.bounds.size.width / 2);
    circCenter.y = (self.bounds.size.height / 2);
    
    if(circCenter.x == centerPoint.x && circCenter.y == centerPoint.y){
        angle = 0.0;
    } else if(circCenter.x == centerPoint.x && centerPoint.y < circCenter.y){
        angle = 0.0;
    } else if(circCenter.x == centerPoint.x && centerPoint.y > circCenter.y){
        angle = 180.0;
    } else if(circCenter.x < circCenter.x && centerPoint.y == circCenter.y){
        angle = 270.0;
    } else if(circCenter.x > circCenter.x && centerPoint.y == circCenter.y){
        angle = 90.0;
    } else {
        double p1 = abs(centerPoint.x - circCenter.x);
        double p2 = abs(centerPoint.y - circCenter.y);
        angle = atan(p1 / p2) * 57.29577951; //multiply it with 1/2 pi
        if(centerPoint.x > circCenter.x && centerPoint.y > circCenter.y) {
            angle = 180 - angle;
        } else if(circCenter.x < circCenter.x && centerPoint.y > circCenter.y) {
            angle = 180 + angle;
        } else if(centerPoint.x < circCenter.x && centerPoint.y < circCenter.y){
            angle = 360 - angle;
        }
    }
    while(angle > 360){
        angle -= 360.0;
    }
    while(angle < 0){
        angle += 360.0;
    }
    
    return angle;
}

- (double)getVertical:(double)distance {
    if(distance > 0.0){
        if([self getAngle] > 270.0 || [self getAngle] < 90.0){
            return distance;
        } else if([self getAngle] > 90.0 || [self getAngle] < 270.0){
            return -distance;
        }
    }
    
    return 0.0;
}

- (CGPoint)getCamera:(double)distance {
    CGPoint cords;
    cords.x = 0.0;
    cords.y = 0.0;
    
    if(distance > 0.0){
        if((([self getAngle] > 315.0 && [self getAngle] < 360.0) || ([self getAngle] > 0.0 && [self getAngle] < 45))){
            //Vertical
            cords.y = -distance;
        } else if([self getAngle] > 135.0 && [self getAngle] < 225.0) {
            cords.y = distance;
        }
        if([self getAngle] > 45.0 && [self getAngle] < 135.0){
            cords.x = distance;
        } else if(([self getAngle] > 225.0 && [self getAngle] < 315.0)){
            cords.x = -distance;
        }
    }
    
    return cords;
}

#pragma mark - Memory management

- (void)dealloc {
    //Dont use [super dealloc], IOS5 NDA problems
}

@end
